﻿#include "CoinUpLayer.h"
#include "utils/MyStringUtils.h"
#include <string>
using namespace std;
using namespace cocos2d;
using namespace cocos2d::ui;//UI命名空间
CoinUpLayer::CoinUpLayer() {

}

CoinUpLayer* CoinUpLayer::create() {
	CoinUpLayer *ret = new (std::nothrow)CoinUpLayer();
	if (ret && ret->init()) {
		ret->autorelease();
		return ret;
	} else {
		CC_SAFE_DELETE(ret);
		return nullptr;
	}
}

bool CoinUpLayer::init() {
	if (!Layer::init()) {
		return false;
	}
	initCSBFile();
	initUserDefault();
	return true;
}

void CoinUpLayer::onExit() {
	Layer::onExit();
	UserDefault::getInstance()->setIntegerForKey("coin_text", coinText);
	UserDefault::getInstance()->setIntegerForKey("profit_text", profitText);

	UserDefault::getInstance()->setIntegerForKey("coin_value", coinValue);
	UserDefault::getInstance()->setIntegerForKey("profit_value", profitValue);

	UserDefault::getInstance()->setIntegerForKey("coin_UpCoinNum", coinUpCoinNum);
	UserDefault::getInstance()->setIntegerForKey("profit_UpCoinNum", profitUpCoinNum);
}

void CoinUpLayer::initCSBFile() {
	rootNode = CSLoader::createNode("scenes/start/layer/CoinUpLayer.csb");
	this->addChild(rootNode);

	coin_text = dynamic_cast<Text*>(rootNode->getChildByName("coin_text"));
	profit_text = dynamic_cast<Text*>(rootNode->getChildByName("profit_text"));

	coin_value = dynamic_cast<TextAtlas*>(rootNode->getChildByName("coin_value"));
	profit_value = dynamic_cast<TextAtlas*>(rootNode->getChildByName("profit_value"));

	coin_UpCoinNum = dynamic_cast<Text*>(rootNode->getChildByName("coin_UpCoinNum"));
	profit_UpCoinNum = dynamic_cast<Text*>(rootNode->getChildByName("profit_UpCoinNum"));

	coinUpbtn = dynamic_cast<Button*>(rootNode->getChildByName("coinUpbtn"));
	
	coinUpbtn->addClickEventListener([this](Ref*) {
		//射速升级逻辑
		_eventDispatcher->dispatchCustomEvent("onCoinUp", this);
		addCoinCoin();
		addCoinLevelAndValue();
	});

	profitUpbtn = dynamic_cast<Button*>(rootNode->getChildByName("profitUpbtn"));
	profitUpbtn->addClickEventListener([this](Ref*) {
		//火力升级逻辑
		_eventDispatcher->dispatchCustomEvent("onProfitUp", this);
		addProfitCoin();
		addProfitLevelAndValue();
	});
}

/*
	加载用户数据
*/
void CoinUpLayer::initUserDefault() {
	char filename[10];
	coinText = UserDefault::getInstance()->getIntegerForKey("coin_text", 1);
	profitText = UserDefault::getInstance()->getIntegerForKey("profit_text", 1);
	sprintf_s(filename, "[Lv %d]", coinText);
	coin_text->setString(filename);
	sprintf_s(filename, "[Lv %d]", profitText);
	profit_text->setString(filename);

	coinValue = UserDefault::getInstance()->getIntegerForKey("coin_value", 100);
	profitValue = UserDefault::getInstance()->getIntegerForKey("profit_value", 5);
	coin_value->setString(to_string(coinValue));
	profit_value->setString(to_string(profitValue));

	coinUpCoinNum = UserDefault::getInstance()->getIntegerForKey("coin_UpCoinNum", 50);
	profitUpCoinNum = UserDefault::getInstance()->getIntegerForKey("profit_UpCoinNum", 50);
	coin_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(coinUpCoinNum));
	profit_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(profitUpCoinNum));
}

void CoinUpLayer::addCoinCoin() {
	coinUpCoinNum += 100;
	coin_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(coinUpCoinNum));
}

void CoinUpLayer::addProfitCoin() {
	profitUpCoinNum += 100;
	profit_UpCoinNum->setString(MyStringUtils::getInstance()->MyStringUtils::getInstance()->coinToString(profitUpCoinNum));
}

void CoinUpLayer::addCoinLevelAndValue() {
	char filename[10];
	coinText += 1;
	sprintf_s(filename, "[Lv %d]", coinText);
	coin_text->setString(filename);

	coinValue += 1;
	coin_value->setString(MyStringUtils::getInstance()->coinToString(coinValue));
}

void CoinUpLayer::addProfitLevelAndValue() {
	char filename[10];
	profitText += 1;
	sprintf_s(filename, "[Lv %d]", profitText);
	profit_text->setString(filename);

	profitValue += 1;
	profit_value->setString(MyStringUtils::getInstance()->coinToString(profitValue));
}